package dev.serbia.tvguide;
 
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.util.ByteArrayBuffer;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TextView;
 
public class ListView_Form extends ListActivity {
	
	TextView lista;
	String myString = null;
	String[] values;
	String data = null;
	ProgressDialog myProgressDialog = null;
	
	Handler mHandler = new Handler();
	
    @Override  
    public void onCreate(Bundle savedInstanceState) 
    {
    	super.onCreate(savedInstanceState);  
        setContentView(R.layout.listview_form);
        
        /* Retrieve the String from the Extra-Bundle 
         * we started this SubActivity with. */ 
        Bundle b = this.getIntent().getExtras(); 
        final String s = b.getString("stanica");
        setTitle(s);
        
        // Display an indeterminate Progress-Dialog 
        myProgressDialog = ProgressDialog.show(ListView_Form.this,      
                     		"Моменат", "Преузимам програм са сервера...", true);
        new Thread()
        	{ 
            public void run()
            	{ 
            	try
            		{
            			URL myURL = new URL("http://www.serbiandownload.com/tv/program/lat/?stanica=" + s);
            			URLConnection ucon = myURL.openConnection();
            			InputStream is = ucon.getInputStream();
            			BufferedInputStream bis = new BufferedInputStream(is);
            			ByteArrayBuffer baf = new ByteArrayBuffer(50);
            			int current = 0;
            		
            			while((current = bis.read()) != -1)
            				{
            					baf.append((byte)current);
            				}

            			// Convert the Bytes read to a String.
            			myString = new String(baf.toByteArray());
             
            			// Show the String on the GUI. 
            			mHandler.post(new Runnable()
            				{
            				public void run()
            					{
            					data = myString; 
            					//Split data into an array  
            					values = data.split("\n");
            					//setListAdapter(new ArrayAdapter(ListView.this, R.layout.listview_style, values));
            					setListAdapter(new ArrayAdapter<String>(ListView_Form.this, R.layout.listview_form_style, values));

            		            }
            		            
            		            public void onListItemClick(ListView_Form parent, View v, int position,
    		long id) {
    		//selection.setText(items[position]);
    		//setTitle("DEDA");
    		}

            					}
            				);

            		} catch (Exception e)
            			{
            				//myString = e.getMessage();
            			}
            		// Dismiss the Dialog 
            		myProgressDialog.dismiss();
            	}
        	}.start(); //NewThread se završava ovde
        	
    } //OnCreate se završava ovde 

}